package com.awesumgames.testgame;

import tv.ouya.console.api.OuyaController;

import com.awesumgames.awesum.AwesumGlobal.Awesum;
import com.awesumgames.awesum.Drawable;
import com.awesumgames.awesum.Game;
import com.awesumgames.awesum.Primitives;
import com.awesumgames.awesum.Vector3;
import com.awesumgames.awesum.Vector4;
import com.awesumgames.testgame.TestScene;
import com.bulletphysics.collision.shapes.SphereShape;

public class Ball extends Drawable {
	Ball(Vector3 position) {
		super();
		pos = position;
	}
	
	public void load() {
		super.load(TraitFlag.Gl.flag | TraitFlag.Phys.flag);

		//////Awesum.Load.Dae(this, R.raw.monkey2);
		
		Vector3 ballDims = new Vector3(.25f, .25f, .25f);
		Primitives.buildRect(this, ballDims, new Vector4(0f, 1f, 0f, 1f), BreakoutLayer.COLL_BALL, BreakoutLayer.MASK_BALL);
		phys.collShape = new SphereShape(.25f);
		phys.init(2.5f, .5f, BreakoutLayer.COLL_BALL, BreakoutLayer.MASK_BALL);
		phys.body.setLinearVelocity(new Vector3(2f, 0f, 0f));
		phys.body.setFriction(.1f);
		phys.body.setDamping(0f, 0f);
		phys.body.setAngularVelocity(new Vector3(0f, 0f, 1f));
	}
	
	private void resetBall() {
		//move.velocity.x = (float)Math.random() * .2f + .1f;
		//move.velocity.y = .2f;
		
		Paddle p = null;
		if (Game.scene instanceof TestScene)
			p = ((TestScene)Game.scene).paddle;
		else if (layer instanceof BreakoutLayer)
			p = ((BreakoutLayer)layer).paddle;
		
		pos.set(p.pos.x + 1f, 1f, 0f);
	}
	
	public void step() {
		//Awesum.debug("ball y = " + pos.y);
		super.step();
		
		//if (origin.y <= 0)
			//move.velocity.y = BOUNCE_VEL;
		/*if (pos.y <= 0f || pos.y >= 8.2f)
			move.velocity.y *= -1;
		if (pos.x <= 0f || pos.x >= 10f - coll.box.size.x)
			move.velocity.x = -move.velocity.x;*/
		
		OuyaController c = OuyaController.getControllerByPlayer(0);
		if (c == null) return;
		if (c.buttonChangedThisFrame(OuyaController.BUTTON_O) && c.buttonPressedThisFrame(OuyaController.BUTTON_O)) {
			//Awesum.debug("ball reset" + c.buttonChangedThisFrame(OuyaController.BUTTON_O));
			//move.velocity.x = .03f;
			//resetBall();
		}
	}
	
	public void collide(Drawable other, Vector3 pointThis, Vector3 pointThat, Vector3 normalThat) {
		Vector3 relP = pointThis.copy().subtract(pos);
		//Awesum.debug("normal: " + normalThat.toString());
		//Awesum.debug("point: " + relP.toString());
		if (other instanceof Paddle) {
			if (normalThat.y > 0f && relP.y < 0f) {
				//switch (((Block)other).type) {
				//	case BOOST:
						phys.body.applyImpulse(new Vector3(0f, 20f, 0f), new Vector3());
				//}
			}
		}
	}
	
	/*public void collide(Drawable other, CollisionFlag side) {
		//(side == CollisionFlag.XPos && move.velocity.x > 0f) || (side == CollisionFlag.XNeg && move.velocity.x < 0f)
		if (side == CollisionFlag.XPos || side == CollisionFlag.XNeg) { 
			move.velocity.x = -move.velocity.x;
		} else if (side == CollisionFlag.YPos) {
			if (move.velocity.y > 0f)
				move.velocity.y = -move.velocity.y;
		} else if (side == CollisionFlag.YNeg) {
			if (other instanceof Block) {
				Block b = (Block)other;
				//if (b.type == Block.Type.BOOST)
				//	move.velocity.y = BOUNCE_VEL * 1.25f;
				//else if (b.type == Block.Type.REDUCE)
				//	move.velocity.y = BOUNCE_VEL * .7f;
				//else
					move.velocity.y = -move.velocity.y;
			} else
				move.velocity.y = BOUNCE_VEL / 2f;
		}
		
		Paddle p = null;
		if (Game.scene instanceof TestScene)
			p = ((TestScene)Game.scene).paddle;
		else if (layer instanceof BreakoutLayer)
			p = ((BreakoutLayer)layer).paddle;
		if (other != p) {
			layer.children.remove(other);
			if (layer.children.size() == 2) //2 = ball and paddle, all blocks are gone
				Game.scene.load();
		}
		
	}*/
}
